#include "manred.h"
#include"gamecontrol.h"

ManRed::ManRed()
{
    this->setPixmap(QPixmap(":/1/Image/ManRedStand.png"));
    this->setPos(70,879);
}

void ManRed::Move()
{
    for(int i=0;i< GameControl::Instance()->mKeyList.size();i++)
    {
        switch( GameControl::Instance()->mKeyList[i])
        {
        case Qt::Key_5:
        {
            this->setPixmap((QPixmap(":/1/Image/ManRedUp.png")));
            jump();
            break;
        }
        case Qt::Key_1://左
        {
            this->setPixmap(QPixmap(":/1/Image/ManRedLeft.png"));
            this->moveBy(-1 * mMoveSpeed,0);
            break;
        }
        case Qt::Key_2:
        {
            this->setPixmap((QPixmap(":/1/Image/ManRedDown.png")));
            down();
            break;
        }
        case Qt::Key_3://右
        {
            this->setPixmap(QPixmap(":/1/Image/ManRedRight.png"));
            this->moveBy(1*mMoveSpeed,0);
            break;
        }
        }
    }
    //检测碰到边界
    boundaryJudge();
    //检测碰到水
    watergreenJudge(70,879);
    water();
}

void ManRed::updatePosition()
{
    if(isJumping)
    {
        vy -= gravity;
        this->setY(this->y()-vy);

    }

    if(this->y()>GameDefine::ScreenHeight-this->boundingRect().height()-30 )
    {
        this->setY(GameDefine::ScreenHeight-this->boundingRect().height()-30);
        vy = 0;
        isJumping = false;
    }
    QRectF mPlayerRect2 = QRectF(this->x()+20,this->y()+25,50,5);

    for(int i=0;i<GameControl::Instance()->platforms.size();i++)
    {
        if(mPlayerRect2.intersects(GameControl::Instance()->platforms[i]))
        {
            vy = -vy;
            break;
        }
    }
}

void ManRed::detectCollisions()//检测下部分碰撞
{
    bool onPlatform = false;

    QRectF mPlayerRect1 = QRectF(this->x()+30,this->y()+80,25,1);

    for(int i=0 ;i<GameControl::Instance()->platforms.size();i++)
    {
        if(mPlayerRect1.intersects(GameControl::Instance()->platforms[i]))
        {
            this->setY(GameControl::Instance()->platforms[i].y()-this->boundingRect().height()+20);
            vy = 0;
            isJumping = false;
            onPlatform = true;
            break;
        }
    }
    if(!onPlatform)
    {
        down();//检测人物是否在平台上 不在就掉下来
    }
}

void ManRed::diamondCollisions()//钻石碰撞
{
    QRectF mManRedRect = QRectF(this->x()+25,this->y()+25,50,50);

    for(int i=0;i<GameControl::Instance()->mDiamondsRed.size();i++)
    {
        if(mManRedRect.intersects(GameControl::Instance()->mDiamondsRed[i]->_Rect))
        {
            QGraphicsScene& gameScene= GameControl::Instance()->getGameScene();
            gameScene.removeItem(GameControl::Instance()->mDiamondsRed[i]);
            delete GameControl::Instance()->mDiamondsRed[i];
            GameControl::Instance()->mDiamondsRed.removeAt(i);
            i--;
        }
    }
}

void ManRed::water()//蓝水
{
    if(this->x()>=863 && this->x()<=968 && this->y() >=875 && this->y() <= 890)
    {
        this->setPos(70,879);
    }
}

void ManRed::changePixmap()//换图片
{
    if(GameControl::Instance()->mKeyList.size() == 0)
    {
        this->setPixmap((QPixmap(":/1/Image/ManRedStand.png")));
    }
}

ManRed::~ManRed()
{

}
